﻿namespace Chain.DAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class MemLevel
    {
        public int Add(Chain.Model.MemLevel model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MemLevel(");
            builder.Append("LevelName,LevelPoint,LevelDiscountPercent,LevelPointPercent,LevellLock,LevelRechargePointRate)");
            builder.Append(" values (");
            builder.Append("@LevelName,@LevelPoint,@LevelDiscountPercent,@LevelPointPercent,@LevellLock,@LevelRechargePointRate)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelName", SqlDbType.NVarChar, 50), new SqlParameter("@LevelPoint", SqlDbType.Int, 4), new SqlParameter("@LevelDiscountPercent", SqlDbType.Float, 8), new SqlParameter("@LevelPointPercent", SqlDbType.Float, 8), new SqlParameter("@LevellLock", SqlDbType.Bit, 1), new SqlParameter("@LevelRechargePointRate", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.LevelName;
            cmdParms[1].Value = model.LevelPoint;
            cmdParms[2].Value = model.LevelDiscountPercent;
            cmdParms[3].Value = model.LevelPointPercent;
            cmdParms[4].Value = model.LevellLock;
            cmdParms[5].Value = model.LevelRechargePointRate;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Delete(int LevelID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemLevel ");
            builder.Append(" where LevelID=@LevelID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LevelID;
            int num = DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
            if (num > 0)
            {
                return num;
            }
            return 0;
        }

        public bool DeleteList(string LevelIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemLevel ");
            builder.Append(" where LevelID in (" + LevelIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int LevelID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemLevel");
            builder.Append(" where LevelID=@LevelID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LevelID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(int LevelID, int LevelPoint)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemLevel");
            builder.Append(" where LevelID <> @LevelID AND LevelPoint = @LevelPoint");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelID", SqlDbType.Int, 4), new SqlParameter("@LevelPoint", SqlDbType.Int) };
            cmdParms[0].Value = LevelID;
            cmdParms[1].Value = LevelPoint;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select LevelID,LevelName,LevelPoint,LevelDiscountPercent,LevelPointPercent,LevellLock,LevelRechargePointRate ");
            builder.Append(" FROM MemLevel ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" ORDER BY LevelPoint asc");
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" LevelID,LevelName,LevelPoint,LevelDiscountPercent,LevelPointPercent,LevellLock,LevelRechargePointRate ");
            builder.Append(" FROM MemLevel ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.LevelID desc");
            }
            builder.Append(")AS Row, T.*  from MemLevel T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetLists(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select LevelID,ShopMemLevelID, LevelName,LevelPoint,ClassDiscountPercent,ClassPointPercent,ClassRechargePointRate,LevellLock from SysShopMemLevel join MemLevel on MemLevel.LevelID=SysShopMemLevel.MemLevelID ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" ORDER BY LevelPoint asc");
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("LevelID", "MemLevel");
        }

        public Chain.Model.MemLevel GetModel(int LevelID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 LevelID,LevelName,LevelPoint,LevelDiscountPercent,LevelPointPercent,LevellLock,LevelRechargePointRate from MemLevel ");
            builder.Append(" where LevelID=@LevelID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LevelID;
            Chain.Model.MemLevel level = new Chain.Model.MemLevel();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["LevelID"] != null) && (set.Tables[0].Rows[0]["LevelID"].ToString() != ""))
            {
                level.LevelID = int.Parse(set.Tables[0].Rows[0]["LevelID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["LevelName"] != null) && (set.Tables[0].Rows[0]["LevelName"].ToString() != ""))
            {
                level.LevelName = set.Tables[0].Rows[0]["LevelName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["LevelPoint"] != null) && (set.Tables[0].Rows[0]["LevelPoint"].ToString() != ""))
            {
                level.LevelPoint = int.Parse(set.Tables[0].Rows[0]["LevelPoint"].ToString());
            }
            if ((set.Tables[0].Rows[0]["LevelDiscountPercent"] != null) && (set.Tables[0].Rows[0]["LevelDiscountPercent"].ToString() != ""))
            {
                level.LevelDiscountPercent = Convert.ToDecimal(set.Tables[0].Rows[0]["LevelDiscountPercent"]);
            }
            if ((set.Tables[0].Rows[0]["LevelPointPercent"] != null) && (set.Tables[0].Rows[0]["LevelPointPercent"].ToString() != ""))
            {
                level.LevelPointPercent = Convert.ToDecimal(set.Tables[0].Rows[0]["LevelPointPercent"]);
            }
            if ((set.Tables[0].Rows[0]["LevellLock"] != null) && (set.Tables[0].Rows[0]["LevellLock"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["LevellLock"].ToString() == "1") || (set.Tables[0].Rows[0]["LevellLock"].ToString().ToLower() == "true"))
                {
                    level.LevellLock = true;
                }
                else
                {
                    level.LevellLock = false;
                }
            }
            if ((set.Tables[0].Rows[0]["LevelRechargePointRate"] != null) && (set.Tables[0].Rows[0]["LevelRechargePointRate"].ToString() != ""))
            {
                level.LevelRechargePointRate = Convert.ToInt32(set.Tables[0].Rows[0]["LevelRechargePointRate"]);
            }
            return level;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MemLevel ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Update(Chain.Model.MemLevel model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MemLevel set ");
            builder.Append("LevelName=@LevelName,");
            builder.Append("LevelPoint=@LevelPoint,");
            builder.Append("LevelDiscountPercent=@LevelDiscountPercent,");
            builder.Append("LevelPointPercent=@LevelPointPercent,");
            builder.Append("LevellLock=@LevellLock,");
            builder.Append("LevelRechargePointRate=@LevelRechargePointRate ");
            builder.Append(" where LevelID=@LevelID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelName", SqlDbType.NVarChar, 50), new SqlParameter("@LevelPoint", SqlDbType.Int, 4), new SqlParameter("@LevelDiscountPercent", SqlDbType.Float, 8), new SqlParameter("@LevelPointPercent", SqlDbType.Float, 8), new SqlParameter("@LevellLock", SqlDbType.Bit, 1), new SqlParameter("@LevelRechargePointRate", SqlDbType.Int, 4), new SqlParameter("@LevelID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.LevelName;
            cmdParms[1].Value = model.LevelPoint;
            cmdParms[2].Value = model.LevelDiscountPercent;
            cmdParms[3].Value = model.LevelPointPercent;
            cmdParms[4].Value = model.LevellLock;
            cmdParms[5].Value = model.LevelRechargePointRate;
            cmdParms[6].Value = model.LevelID;
            int num = DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
            if (num > 0)
            {
                return num;
            }
            return 0;
        }
    }
}

